package com.etone.app.crawler.portals.handle;

import com.etone.app.crawler.portals.common.TaskQueue;
import com.etone.app.crawler.portals.entity.OutputEntity;
import com.etone.app.crawler.portals.entity.Task;
import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author <a href="mailto:cbean@qq.com">CowboyBean</a>
 * @date Mar 14, 2014
 * @version $$Reversion$$
 */
public class TaskHandleThread extends BaseHandle {

    private final static String userAgent = "User-Agent:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
    private final static int maxLevel = 3;

    private static final Logger logger = Logger.getLogger(TaskHandleThread.class);

    @Override
    public void run() {

        while (true) {

            // 提取任务
            Task task = TaskQueue.getTask();

            // ID || 名称 || URL || NowUrl || NowHost || 当前页面等级 || 连接来源页 
            // || UserAgent || 处理结果 || Title || Keywords || Description
            try {

                String url = task.getUrl();
                Connection conn = Jsoup.connect(url);
                Response response = conn.userAgent(userAgent).timeout(10000).execute();
                Document doc = response.parse();

                // 构造输出实体
                OutputEntity output = new OutputEntity();

                output.setServiceName(task.getServiceName());
                output.setKeywords(doc.select("meta[name=keywords]").attr("content"));
                output.setDescription(doc.select("meta[name=description]").attr("content"));
                output.setSrcUrl(task.getSrcUrl());
                output.setLevel(task.getLevel() + "");
                output.setUrl(url);
                output.setTitle(doc.title());
                output.setUserAgent("IOS");
                output.setHttpCode(response.statusCode() + "");
                output.setHost(response.url().getHost());
                output.setServiceUrl(task.getServiceUrl());
                output.setText(task.getText());

                write(output);

                // 查找下一级url
                if (task.getLevel() < maxLevel) {
                    Elements aTags = doc.getElementsByTag("a");
                    for (Element aTag : aTags) {
                        Task newTask = new Task();
                        newTask.setLevel(task.getLevel() + 1);
                        newTask.setServiceName(task.getServiceName());
                        newTask.setServiceUrl(task.getServiceUrl());
                        newTask.setSrcUrl(task.getUrl());
                        newTask.setText(aTag.text());
                        String subUrl = aTag.attr("href");
                        String uri = doc.baseUri();
                        newTask.setUrl(subUrl.startsWith("http") ? subUrl : uri + subUrl);
                        TaskQueue.putTask(newTask);
                    }
                }

            } catch (Exception ex) {
                logger.error("爬取页面信息时出错。", ex);
            }

        }

    }

}
